package com.ewhale.feitengguest.ui.user.mine;

import android.Manifest;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ewhale.feitengguest.R;
import com.ewhale.feitengguest.dto.MyTaskDetailDto;
import com.ewhale.feitengguest.dto.TaskDetailsDto;
import com.ewhale.feitengguest.presenter.user.mine.MyTaskDetailPresenter;
import com.ewhale.feitengguest.ui.user.mine.adapter.ContentAdapter;
import com.ewhale.feitengguest.ui.user.mine.adapter.ShowImageAdapter;
import com.ewhale.feitengguest.ui.user.task.adapter.PublicPicAdapter;
import com.ewhale.feitengguest.ui.user.task.adapter.StepListAdapter;
import com.ewhale.feitengguest.utils.GlideImageLoader;
import com.ewhale.feitengguest.utils.PictureUtils;
import com.ewhale.feitengguest.utils.QiniuUtils;
import com.ewhale.feitengguest.view.user.mine.MyTaskDetailView;
import com.ewhale.feitengguest.widget.BannerLayout;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;
import com.simga.library.activity.MBaseActivity;
import com.simga.library.utils.CheckUtil;
import com.simga.library.utils.DateUtil;
import com.simga.library.utils.StringUtil;
import com.simga.library.utils.glide.GlideUtil;
import com.simga.library.widget.BGButton;
import com.simga.library.widget.HintDialog;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import cc.shinichi.library.ImagePreview;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

public class MyTaskDetailActivity extends MBaseActivity<MyTaskDetailPresenter> implements MyTaskDetailView, EasyPermissions.PermissionCallbacks {
    private static final int REQUEST_CODE_PERMISSION_2 = 10002;
    private static final int REQUEST_CODE_CHOOSE_IMAGE = 1000;
    @BindView(R.id.bannerlayout)
    BannerLayout mBannerlayout;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_price)
    TextView mTvPrice;
    @BindView(R.id.tv_over_hint)
    TextView mTvOverHint;
    @BindView(R.id.tv_over)
    TextView mTvOver;
    @BindView(R.id.tv_hour)
    TextView mTvHour;
    @BindView(R.id.tv_minute)
    TextView mTvMinute;
    @BindView(R.id.tv_second)
    TextView mTvSecond;
    @BindView(R.id.ll_time_remaining)
    LinearLayout mLlTimeRemaining;
    @BindView(R.id.iv_tast_status)
    ImageView mIvTastStatus;
    @BindView(R.id.tv_cate)
    TextView mTvCate;
    @BindView(R.id.tv_authTime)
    TextView mTvAuthTime;
    @BindView(R.id.tv_device)
    TextView mTvDevice;
    @BindView(R.id.rb_step)
    RadioButton mRbStep;
    @BindView(R.id.rb_message)
    RadioButton mRbMessage;
    @BindView(R.id.rb_type_tab)
    RadioGroup mRbTypeTab;
    @BindView(R.id.rb_step_line)
    ImageView mRbStepLine;
    @BindView(R.id.rb_message_line)
    ImageView mRbMessageLine;
    @BindView(R.id.ll_bottom_line)
    LinearLayout mLlBottomLine;
    @BindView(R.id.listview)
    RecyclerView mListview;
    @BindView(R.id.tv_up_hand_card)
    TextView mTvUpHandCard;
    @BindView(R.id.gv_image)
    RecyclerView mGvImage;
    @BindView(R.id.cl_upload_info)
    ConstraintLayout mClUploadInfo;
    @BindView(R.id.tv_pic_info)
    TextView mTvPicInfo;
    @BindView(R.id.gv_image_msg)
    RecyclerView mGvImageMsg;
    @BindView(R.id.tv_fail_message)
    TextView mTvFailMessage;
    @BindView(R.id.cl_show_info)
    ConstraintLayout mClShowInfo;
    @BindView(R.id.btn_handle)
    BGButton mBtnHandle;
    @BindView(R.id.scrollView)
    NestedScrollView mScrollView;
    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.tv_toobar)
    TextView mTvToobar;
    @BindView(R.id.rl_toobar)
    RelativeLayout mRlToobar;
    @BindView(R.id.iv_taskQRCode)
    ImageView mIvTaskQRCode;
    @BindView(R.id.tv_linkText)
    TextView mTvLinkText;
    @BindView(R.id.ll_qrCode)
    LinearLayout mLlQrCode;
    @BindView(R.id.lv_etContent)
    RecyclerView mLvEtContent;
    @BindView(R.id.lv_showContent)
    RecyclerView mLvShowContent;
    @BindView(R.id.btn_cancel)
    BGButton btnCancel;

    private PublicPicAdapter mAdapter;
    private List<String> images = new ArrayList<>();//上传图片的列表
    private List<String> imageUrl = new ArrayList<>();//轮播图列表
    private List<String> showImage = new ArrayList<>();//提交后展示的图片
    private List<TaskDetailsDto.MissionStepDtosBean> steplist = new ArrayList<>();//轮播图列表
    private StepListAdapter mStepAdapter;
    private ShowImageAdapter mImageAdaoter;
    private long missionId;
    private MyTime mMyTime;
    private MyTaskDetailDto detailDto;
    private ContentAdapter contentAdapter;
    private List<MyTaskDetailDto.ContentExplainDtosBean> contentList = new ArrayList<>();
    private HintDialog mHintDialog;


    public static void open(MBaseActivity context, long missionId) {
        Bundle bundle = new Bundle();
        bundle.putLong("missionId", missionId);
        context.startActivity(bundle, MyTaskDetailActivity.class);
    }

    @Override
    protected void onGetBundle(Bundle bundle) {
        missionId = bundle.getLong("missionId");
    }

    @Override
    protected int getViewId() {
        return R.layout.activity_mytask_details;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        mListview.setNestedScrollingEnabled(false);
        mBannerlayout.setImageLoader(new GlideImageLoader());

        mListview.setLayoutManager(new LinearLayoutManager(mContext));
        mListview.setHasFixedSize(true);
        mStepAdapter = new StepListAdapter(steplist);
        mListview.setAdapter(mStepAdapter);

        mAdapter = new PublicPicAdapter(images, 5);
        mGvImage.setAdapter(mAdapter);
        LinearLayoutManager manager = new GridLayoutManager(mContext, 3);
        mGvImage.setLayoutManager(manager);

        mImageAdaoter = new ShowImageAdapter(showImage, false);
        mGvImageMsg.setAdapter(mImageAdaoter);
        GridLayoutManager manager1 = new GridLayoutManager(mContext, 3);
        mGvImageMsg.setLayoutManager(manager1);

        contentAdapter = new ContentAdapter(contentList, true);
        mLvEtContent.setAdapter(contentAdapter);
        mLvEtContent.setLayoutManager(new LinearLayoutManager(mContext));
        mLvEtContent.setHasFixedSize(true);

        contentAdapter = new ContentAdapter(contentList, false);
        mLvShowContent.setAdapter(contentAdapter);
        mLvShowContent.setLayoutManager(new LinearLayoutManager(mContext));
        mLvShowContent.setHasFixedSize(true);

        presenter.loadMyTaskDetail(missionId);
    }

    @Override
    protected boolean isUseBaseTitleBar() {
        return false;
    }

    @Override
    protected void initListener() {
        mIvBack.setOnClickListener(v -> finish());
        mRbTypeTab.setOnCheckedChangeListener((radioGroup, i) -> {
            if (i == R.id.rb_step) {
                if (!CheckUtil.isNull(detailDto.getMissionQrcode())) {
                    mIvTaskQRCode.setVisibility(View.VISIBLE);
                }
                if (!CheckUtil.isNull(detailDto.getMissionUrl())) {
                    mTvLinkText.setVisibility(View.VISIBLE);
                }
                if (!CheckUtil.isNull(detailDto.getMissionUrl()) || !CheckUtil.isNull(detailDto.getMissionQrcode())) {
                    mLlQrCode.setVisibility(View.VISIBLE);
                }
                mRbStepLine.setVisibility(View.VISIBLE);
                mRbMessageLine.setVisibility(View.INVISIBLE);
                mListview.setVisibility(View.VISIBLE);
                mClShowInfo.setVisibility(View.GONE);
                mBtnHandle.setVisibility(View.GONE);
            } else {
                mLlQrCode.setVisibility(View.GONE);
                mIvTaskQRCode.setVisibility(View.GONE);
                mTvLinkText.setVisibility(View.GONE);
                mRbStepLine.setVisibility(View.INVISIBLE);
                mRbMessageLine.setVisibility(View.VISIBLE);
                mListview.setVisibility(View.GONE);
                mClShowInfo.setVisibility(View.VISIBLE);
                if (detailDto.getAccountMissionDto().getStatus() == 2) {//审核失败
                    mBtnHandle.setVisibility(View.VISIBLE);
                } else {
                    mBtnHandle.setVisibility(View.GONE);
                }
            }
        });
        mScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (scrollY <= 0) {
                    mRlToobar.setBackgroundColor(Color.argb((int) 0, 250, 192, 52));
                    mTvToobar.setVisibility(View.INVISIBLE);
                } else if (scrollY > 0 && scrollY <= mRlToobar.getHeight()) {
                    mTvToobar.setVisibility(View.VISIBLE);
                    float scale = (float) scrollY / mRlToobar.getHeight();
                    float alpha = (255 * scale);
                    mRlToobar.setBackgroundColor(Color.argb((int) alpha, 250, 192, 52));
                } else {
                    mTvToobar.setVisibility(View.VISIBLE);
                    mRlToobar.setBackgroundColor(Color.argb((int) 255, 250, 192, 52));
                }
            }
        });
        mBtnHandle.setOnClickListener(v -> {
            if (mBtnHandle.getText().toString().equals("去提交审核")) {
                mBtnHandle.setText("确认提交");
                mListview.setVisibility(View.GONE);
                mClUploadInfo.setVisibility(View.VISIBLE);
                mRbTypeTab.setVisibility(View.GONE);
                mLlBottomLine.setVisibility(View.GONE);
            } else if (mBtnHandle.getText().toString().equals("确认提交")) {
                StringBuffer buffer = new StringBuffer();
                if (detailDto.getSubmitType() == 1) {//图文
                    for (String str : images) {
                        buffer.append(str);
                        buffer.append(",");
                    }
                    if (CheckUtil.isNull(buffer.toString())) {
                        showToast("请先完善提交内容");
                        return;
                    }
                    for (MyTaskDetailDto.ContentExplainDtosBean bean : detailDto.getContentExplainDtos()) {
                        if (CheckUtil.isNull(bean.getContent())) {
                            showToast("请先完善提交内容");
                            return;
                        }
                    }
                } else if (detailDto.getSubmitType() == 2) {//文字
                    for (MyTaskDetailDto.ContentExplainDtosBean bean : detailDto.getContentExplainDtos()) {
                        if (CheckUtil.isNull(bean.getContent())) {
                            showToast("请先完善提交内容");
                            return;
                        }
                    }
                } else {//图片
                    for (String str : images) {
                        buffer.append(str);
                        buffer.append(",");
                    }
                    if (CheckUtil.isNull(buffer.toString())) {
                        showToast("请先完善提交内容");
                        return;
                    }
                }
                presenter.commitMessage(missionId, detailDto.getContentExplainDtos(),
                        CheckUtil.isNull(buffer.toString()) ? "" : buffer.toString().substring(0, buffer.toString().length() - 1));
            } else if (mBtnHandle.getText().toString().equals("重新提交")) {
                for (MyTaskDetailDto.ContentExplainDtosBean bean : detailDto.getContentExplainDtos()) {
                    bean.setContent("");
                }
                contentAdapter.notifyDataSetChanged();
                mBtnHandle.setText("确认提交");
                mListview.setVisibility(View.GONE);
                mClUploadInfo.setVisibility(View.VISIBLE);
                mRbTypeTab.setVisibility(View.GONE);
                mLlBottomLine.setVisibility(View.GONE);
                mClShowInfo.setVisibility(View.GONE);
            }
        });
        mAdapter.setOnItemClickListener(new PublicPicAdapter.onItemClickListener() {
            @Override
            public void onDelete(int position) {
                images.remove(position);
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onItemClick(int position) {
                if (images.size() == 3) {
                    return;
                }
                if (position == images.size()) {
                    requestPermissionMain();
                }
            }
        });
        mIvTaskQRCode.setOnClickListener(v -> {
            if (CheckUtil.isNull(detailDto.getMissionQrcode())) return;
            ImagePreview.getInstance().setContext(mContext)
                    .setShowDownButton(false)
                    .setImage(detailDto.getMissionQrcode())
                    .setLoadStrategy(ImagePreview.LoadStrategy.AlwaysOrigin)
                    .setZoomTransitionDuration(300)
                    .setEnableDragClose(true)
                    .setErrorPlaceHolder(R.drawable.default_image).start();
        });
        btnCancel.setOnClickListener(V -> {
            mHintDialog = new HintDialog(mContext, "提示", "您确定要取消该任务？", new String[]{"取消", "确定"});
            mHintDialog.setCallback(new HintDialog.Callback() {
                @Override
                public void callback() {
                    presenter.cancelTask(missionId);
                }

                @Override
                public void cancle() {
                }
            });
            mHintDialog.show();
        });
    }

    @Override
    public void cancelTaskSuccess() {
        showToast("取消成功");
        presenter.loadMyTaskDetail(missionId);
    }

    @AfterPermissionGranted(REQUEST_CODE_PERMISSION_2)
    private void requestPermissionMain() {
        String[] perms = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA};
        if (EasyPermissions.hasPermissions(mContext, perms)) {
            PictureUtils.openAluamMore(this, REQUEST_CODE_CHOOSE_IMAGE, 5 - images.size());
        } else {
            EasyPermissions.requestPermissions(this, "申请获取相关权限",
                    REQUEST_CODE_PERMISSION_2, perms);
        }
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        PictureUtils.openAluamMore(this, requestCode, 5 - images.size());
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            new AppSettingsDialog.Builder(this).setTitle("权限申请")
                    .setRationale("申请获取相关权限")
                    .setPositiveButton(getString(R.string.comfir))
                    .setNegativeButton(getString(R.string.cancle))
                    .build()
                    .show();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        if (requestCode == REQUEST_CODE_CHOOSE_IMAGE) {
            showProLoading();
            List<String> image = new ArrayList<>();
            for (LocalMedia media : PictureSelector.obtainMultipleResult(data)) {
                image.add(media.getCompressPath());
            }
            showLoading();
            QiniuUtils.uploadImage(image, new QiniuUtils.onUploadImageCallBack() {
                @Override
                public void onResult(List<String> keys) {
                    dismissLoading();
                    images.addAll(keys);
                    mAdapter.notifyDataSetChanged();
                    PictureUtils.clearCache(mContext);
                }
            });
        }
    }

    @Override
    public void showProLoading() {
        showLoading();
    }

    @Override
    public void dimissProLoading() {
        dismissLoading();
    }

    @Override
    public void showErrorMessage(int postCode, String code, String message) {
        showErrorMsg(code, message);
    }


    @Override
    public void showTaskDetails(MyTaskDetailDto result) {
        detailDto = result;
        imageUrl.clear();
        imageUrl.addAll(Arrays.asList(result.getImg().split(",")));
        mBannerlayout.setImages(imageUrl);
        mBannerlayout.start();
        mTvTitle.setText(result.getTitle());
        mTvPrice.setText("￥" + StringUtil.to2Decimal(StringUtil.to2Double(result.getUnitPrice())));
        mTvCate.setText(result.getClassificationName());
        mTvAuthTime.setText(result.getAuditHour() + "小时内审核");
        mTvOver.setText(result.getEndTimeStr());
        if (result.getDeviceType() == 1) {
            mTvDevice.setText("全部");
        } else if (result.getDeviceType() == 2) {
            mTvDevice.setText("IOS");
        } else if (result.getDeviceType() == 3) {
            mTvDevice.setText("Android");
        }

        steplist.clear();
        steplist.addAll(result.getMissionStepDtos());
        mStepAdapter.notifyDataSetChanged();

        contentList.clear();
        if (result.getContentExplainDtos().size() > 0) {
            for (int i = 0; i < result.getContentExplainDtos().size(); i++) {
                if (i == 0) {
                    result.getContentExplainDtos().get(i).setContent(result.getAccountMissionDto().getContent());
                } else if (i == 1) {
                    result.getContentExplainDtos().get(i).setContent(result.getAccountMissionDto().getContent2());
                } else if (i == 2) {
                    result.getContentExplainDtos().get(i).setContent(result.getAccountMissionDto().getContent3());
                } else if (i == 3) {
                    result.getContentExplainDtos().get(i).setContent(result.getAccountMissionDto().getContent4());
                } else if (i == 4) {
                    result.getContentExplainDtos().get(i).setContent(result.getAccountMissionDto().getContent5());
                }
            }
        }
        contentList.addAll(result.getContentExplainDtos());
        contentAdapter.notifyDataSetChanged();

        if (!CheckUtil.isNull(result.getMissionQrcode())) {
            GlideUtil.loadPicture(result.getMissionQrcode(), mIvTaskQRCode, R.drawable.default_image);
            mIvTaskQRCode.setVisibility(View.VISIBLE);
        } else {
            mIvTaskQRCode.setVisibility(View.GONE);
        }
        if (!CheckUtil.isNull(result.getMissionUrl())) {
            mTvLinkText.setVisibility(View.VISIBLE);
            mTvLinkText.setText(result.getMissionUrl());
        } else {
            mTvLinkText.setVisibility(View.GONE);
        }
        if (!CheckUtil.isNull(result.getMissionUrl()) || !CheckUtil.isNull(result.getMissionQrcode())) {
            mLlQrCode.setVisibility(View.VISIBLE);
        } else {
            mLlQrCode.setVisibility(View.GONE);
        }

        mTvPicInfo.setText(result.getImgExplain());
        mTvUpHandCard.setText(result.getImgExplain());

        if (result.getSubmitType() == 1) {//图文
            mTvUpHandCard.setVisibility(View.VISIBLE);
            mTvPicInfo.setVisibility(View.VISIBLE);
            mGvImage.setVisibility(View.VISIBLE);
            mGvImageMsg.setVisibility(View.VISIBLE);
        } else if (result.getSubmitType() == 2) {//文字
            mTvPicInfo.setVisibility(View.GONE);
            mTvUpHandCard.setVisibility(View.GONE);
            mGvImage.setVisibility(View.GONE);
            mGvImageMsg.setVisibility(View.GONE);
        } else {//图片
            mTvPicInfo.setVisibility(View.VISIBLE);
            mTvUpHandCard.setVisibility(View.VISIBLE);
            mGvImage.setVisibility(View.VISIBLE);
            mGvImageMsg.setVisibility(View.VISIBLE);
        }
        if (result.getAccountMissionDto().getStatus() == 0) {//未提交
            if (result.getEndTimeStamp() * 1000 - new Date().getTime() > 0) {
                long countDown = (result.getEndTimeStamp() * 1000 - new Date().getTime()) / 1000;
                mMyTime = new MyTime(countDown * 1000, 1000);
                mMyTime.start();
                mLlTimeRemaining.setVisibility(View.VISIBLE);
                mIvTastStatus.setVisibility(View.GONE);
                mBtnHandle.setText("去提交审核");
                btnCancel.setVisibility(View.VISIBLE);
                mBtnHandle.setVisibility(View.VISIBLE);
                contentList.clear();
                contentList.addAll(result.getContentExplainDtos());
                contentAdapter.notifyDataSetChanged();
            } else {
                mLlTimeRemaining.setVisibility(View.GONE);
                mIvTastStatus.setVisibility(View.VISIBLE);
                mIvTastStatus.setImageResource(R.mipmap.d3_ic_ygq);
            }
        } else {//已提交或者已过期
            btnCancel.setVisibility(View.GONE);
            mLlTimeRemaining.setVisibility(View.GONE);
            mIvTastStatus.setVisibility(View.VISIBLE);
            mRbTypeTab.check(R.id.rb_step);
            mClUploadInfo.setVisibility(View.GONE);
            mRbTypeTab.setVisibility(View.VISIBLE);
            mLlBottomLine.setVisibility(View.VISIBLE);
            mRbMessage.setVisibility(View.VISIBLE);
            mRbMessageLine.setVisibility(View.INVISIBLE);
            mListview.setVisibility(View.VISIBLE);
            mClShowInfo.setVisibility(View.GONE);
            mTvFailMessage.setVisibility(View.GONE);
            mBtnHandle.setVisibility(View.GONE);
            if (result.getAccountMissionDto().getStatus() == 1) {//待审核
                mIvTastStatus.setImageResource(R.mipmap.d3_ic_shz);
                mBtnHandle.setVisibility(View.GONE);
            } else if (result.getAccountMissionDto().getStatus() == 2) {//审核失败
                mIvTastStatus.setImageResource(R.mipmap.d3_ic_shbtg);
                mTvFailMessage.setVisibility(View.VISIBLE);
                mTvFailMessage.setText("不通过原因：" + result.getAccountMissionDto().getRefuseCause());
                if (result.getEndTimeStamp() * 1000 - new Date().getTime() > 0) {
                    mBtnHandle.setText("重新提交");
                    mBtnHandle.setEnabled(true);
                    mBtnHandle.setNormalSolid(ContextCompat.getColor(mContext, R.color.main_color));
                } else {
                    mBtnHandle.setVisibility(View.GONE);
                    mBtnHandle.setEnabled(false);
                    mBtnHandle.setText("任务已过期");
                    mBtnHandle.setNormalSolid(Color.parseColor("#bbbbbb"));
                }
            } else if (result.getAccountMissionDto().getStatus() == 3) {//审核通过
                mIvTastStatus.setImageResource(R.mipmap.d3_ic_shtg);
            } else {//过期
                mIvTastStatus.setImageResource(R.mipmap.d3_ic_ygq);
                mRbMessage.setVisibility(View.GONE);
                mRbMessageLine.setVisibility(View.GONE);
            }
            if (!CheckUtil.isNull(result.getAccountMissionDto().getImgUrls())) {
                showImage.clear();
                showImage.addAll(Arrays.asList(result.getAccountMissionDto().getImgUrls().split(",")));
                mImageAdaoter.notifyDataSetChanged();
            }
        }
    }

    private class MyTime extends CountDownTimer {
        public MyTime(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            String overTime = DateUtil.formatSeconds2(millisUntilFinished / 1000);
            int count = 0;
            Pattern pattern = Pattern.compile(":");// 通过静态方法compile(String regex)方法来创建,将给定的正则表达式编译并赋予给Pattern类
            Matcher matcher = pattern.matcher(overTime);//
            while (matcher.find()) {// boolean find() 对字符串进行匹配,匹配到的字符串可以在任何位置
                count++;
            }
            if (count == 0) {
                mTvHour.setText("00");
                mTvMinute.setText("00");
                mTvSecond.setText(String.valueOf(millisUntilFinished / 1000));
            } else if (count == 1) {//即代表只有分秒
                mTvHour.setText("00");
                mTvMinute.setText(overTime.substring(0, overTime.indexOf(":")));
                mTvSecond.setText(overTime.substring(overTime.indexOf(":") + 1, overTime.lastIndexOf(":")));
            } else if (count == 2) {//即代表只有时分秒
                mTvHour.setText(overTime.substring(0, overTime.indexOf(":")));
                mTvMinute.setText(overTime.substring(overTime.indexOf(":") + 1, overTime.lastIndexOf(":")));
                mTvSecond.setText(overTime.substring(overTime.lastIndexOf(":") + 1, overTime.length()));
            }
        }

        @Override
        public void onFinish() {
            mLlTimeRemaining.setVisibility(View.GONE);
            mIvTastStatus.setVisibility(View.VISIBLE);
            mIvTastStatus.setImageResource(R.mipmap.d3_ic_ygq);
            mBtnHandle.setVisibility(View.GONE);
        }
    }

    @Override
    public void commitSuccess() {
        showToast("提交成功，等待审核");
        presenter.loadMyTaskDetail(missionId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mMyTime != null)
            mMyTime.cancel();
        mMyTime = null;
    }
}